package com.assignwork.service.impl;

import com.assignwork.dto.TrainStatisticsDTO;
import com.assignwork.entity.TrainStatisticsItem;
import com.assignwork.service.ITrainStatisticsItemService;
import com.assignwork.vo.TrainStatisticsItemVO;
import com.assignwork.vo.TrainStatisticsVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import org.springblade.core.mp.base.BaseServiceImpl;
import com.assignwork.entity.TrainStatistics;
import com.assignwork.mapper.TrainStatisticsMapper;
import com.assignwork.service.ITrainStatisticsService;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (TrainStatistics)表服务实现类
 */
@AllArgsConstructor
@Service("trainStatisticsService")
public class TrainStatisticsServiceImpl extends BaseServiceImpl<TrainStatisticsMapper, TrainStatistics> implements ITrainStatisticsService {

    ITrainStatisticsItemService itemService;

    @Override
    public boolean saveOrUpdateTrainStatistics(BladeUser bladeUser, TrainStatisticsDTO dto) {
        this.saveOrUpdate(dto);
        itemService.remove(new QueryWrapper<TrainStatisticsItem>().eq("parent_id", dto.getId()));
        return itemService.saveDtos(dto.getData());
    }

    @Override
    public TrainStatisticsVO detailTrainStatistics(LocalDate planDate) {
        TrainStatistics one = this.lambdaQuery()
                .eq(TrainStatistics::getPlanDate, planDate)
                .orderByDesc(TrainStatistics::getUpdateTime)
                .last("limit 1")
                .one();

        TrainStatistics yesterday = this.lambdaQuery()
                .eq(TrainStatistics::getPlanDate, planDate.minusDays(1))
                .orderByDesc(TrainStatistics::getUpdateTime)
                .last("limit 1")
                .one();

        List<TrainStatisticsItemVO> syzc = new ArrayList<>();
        if (yesterday != null) {
            List<TrainStatisticsItemVO> vos = itemService.listByParentId(yesterday.getId());
            syzc = vos
                    .stream()
                    .filter(TrainStatisticsItemVO::isSYZC)
                    .map(TrainStatisticsItemVO::toSZYSC)
                    .collect(Collectors.toList());
        }

        if (one != null) {
            TrainStatisticsVO vo = Func.copy(one, TrainStatisticsVO.class);
            List<TrainStatisticsItemVO> items = itemService.listByParentId(one.getId());
            List<TrainStatisticsItemVO> res = items.stream()
                    .filter(TrainStatisticsItemVO::notSZYSC)
                    .collect(Collectors.toList());
            res.addAll(syzc);
            vo.setData(res);
            return vo;
        } else {
            TrainStatisticsVO vo = new TrainStatisticsVO();
            vo.setData(syzc);
            return vo;
        }
    }

}
